<?php
namespace App\Http\Controllers;
use App\Exceptions\ValidateException;
use App\Model\Setting;
use App\Repository\Contracts\AdminInterface;
use App\Repository\Contracts\SettingInterface;
use App\Repository\Contracts\ShiftExchangeInterface;
use App\Support\Helper\CommonHelper;
use Illuminate\Http\Request;
use Auth;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-02-24 14:00
 *
 * @Description:
 */
class ShiftExchangeController extends Controller
{
    /**
     * @var Request
     */
    protected $request;

    /**
     * @var AdminInterface
     */
    protected $admin;

    /**
     * @var ShiftExchangeInterface
     */
    protected $shiftExchange;

    protected $uid;

    protected $storeId;

    /**
     * init
     *
     * ShiftExchangeController constructor.
     * @param Request $request
     * @param AdminInterface $admin
     * @param ShiftExchangeInterface $shiftExchange
     */
    public function __construct(Request $request, AdminInterface $admin, ShiftExchangeInterface $shiftExchange)
    {
        $this->request = $request;

        $this->admin = $admin;

        $this->shiftExchange = $shiftExchange;

        $this->uid = CommonHelper::getToken(Auth::user())['uid'];

        $this->storeId = CommonHelper::getToken(Auth::user())['storeId'];
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
       // print_r($this->request);
        $limit = $this->request->input('limit', 15);
        $page = $this->request->input('page', 1);
        $uid = $this->request->input('uid', null);
        //Search Map
        $searchMap = $this->searchMap([
            ['uid', $uid, '=']
        ]);
//        print_r($this->request);die;
        $list = $this->shiftExchange->lists($limit, $page, $searchMap);
       // print_r($list);die;
        return $this->success($list);
    }

    /**
     * Login
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function login()
    {
        try {
            $data = $this->request->only(['phone', 'password']);
//            print_r($data);
            $userInfo = $this->admin->signIn($data);
            $userInfo['storeConfig'] = json_decode(app(SettingInterface::class)->findByName(Setting::STORE_CONFIG)['config'], true);
//            print_r($userInfo);exit;
            return $this->success($userInfo);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * Shift Exchange Check
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function check()
    {
        try {
            $password = $this->request->input('password');
            $userInfo = $this->admin->check($this->uid, $password);

            return $this->success($userInfo);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function inventory()
    {
        try {
            $uid = $this->uid;
            $number = $this->request->input('number');
            $detail = $this->shiftExchange->inventory($uid, $number);
            $detail['userInfo']['number'] = $number;

            return $this->success($detail);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail($id)
    {
        try {
            $detail = $this->shiftExchange->detail($id);

            return $this->success($detail);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function exchange()
    {
        try {
            $uid = $this->uid;
            $number = $this->request->input('number');
            $detail = $this->shiftExchange->updateByNumber($uid, $number);

            return $this->success($detail);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }
}